package com.jrx.anytxn.reporting.job.xib.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.param.utils.BizTypeProducts;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.entity.TlFreeInterestTable;
import com.jrx.anytxn.reporting.job.xib.step.FreeInterestReportingListener;
import com.jrx.anytxn.reporting.job.xib.step.FreeInterestReportingProcessor;
import com.jrx.anytxn.reporting.job.xib.step.FreeInterestReportingWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @comment: 免息成本报表任务定义
 * @author: liuzb
 * @date: 2021/9/22 15:00
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class FreeInterestReportingConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    FreeInterestReportingListener freeInterestReportingListener;
    @Autowired
    FreeInterestReportingProcessor freeInterestReportingProcessor;
    @Autowired
    FreeInterestReportingWriter freeInterestReportingWriter;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Bean
    public Job xibFreeInterestReportingJob() {
        return jobBuilderFactory.get("xibFreeInterestReportingJob")
                .incrementer(new RunIdIncrementer())
                .listener(freeInterestReportingListener)
                .start(xibFreeInterestReportingStep())
                .build();
    }

    @Bean
    public Step xibFreeInterestReportingStep() {
        return stepBuilderFactory.get("xibFreeInterestReportingStep")
                .<TlFreeInterestTable, TlFreeInterestTable>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                .reader(freeInterestReportingReader())
                .processor(freeInterestReportingProcessor)
                .writer(freeInterestReportingWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlFreeInterestTable> freeInterestReportingReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
        MyBatisCursorItemReader<TlFreeInterestTable> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        //信用就医替换 用 selectFreeInterestWithProducts 替换 selectFreeInterest
        cursorItemReader.setQueryId("com.jrx.anytxn.reporting.mapper.ext.ExtTlFreeInterestTableMapper.selectFreeInterestWithProducts");
        Map<String, Object> map = new HashMap<>();
        map.put("statusList", Arrays.asList(ReportingConstant.ACCOUNT_STATUS_1,ReportingConstant.ACCOUNT_STATUS_2));
        map.put("payOffStatus", Arrays.asList(ReportingConstant.ACCOUNT_STATUS_6,ReportingConstant.ACCOUNT_STATUS_7,ReportingConstant.ACCOUNT_STATUS_8));
        map.put("loanType", ReportingConstant.LOAN_TYPE_4);
        map.put("today", org.getToday());
        map.put("productIds", BizTypeProducts.getXAHSeriesProducts());
        cursorItemReader.setParameterValues(map);
        SynchronizedItemStreamReader<TlFreeInterestTable> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

}
